﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using Sharp.DataTransferObject;
using Sharp.Domain.PlatformBoundedContext.PlatformModule.Aggregates;
using Sharp.Domain.Repositories;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Search;
using Sharp.ServiceContracts;

namespace Sharp.Application.Platform
{
    public class PlatformServiceImpl : ApplicationService,IPlatformService
    {
        private readonly ISubSystemRepository _subSystemRepository;

        #region 构造函数


        /// <summary>
        ///     初始化一个新的<c>SystemServiceImpl</c>实例。
        /// </summary>
        /// <param name="context"></param>
        /// <param name="subSystemRepository"></param>
        public PlatformServiceImpl(IRepositoryContext context, ISubSystemRepository subSystemRepository)
            : base(context)
        {
            _subSystemRepository = subSystemRepository;
        }

        #endregion

        #region 下级系统相关
        #region 保存子系统
        /// <summary>
        /// 保存子系统
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Logging("保存子系统“{@p0.Name}”")]
        public Result SaveSubSystem(SubSystemDTO model)
        {
            Result res = new Result();
            var info = model.To<SubSystem>();
            if (info.Id.IsEmpty())
            {
                _subSystemRepository.Add(info);
            }
            else
            {
                _subSystemRepository.Update(info);
            }
            Context.Commit();
            return res;
        }
        #endregion

        #region 删除子系统
        /// <summary>
        /// 删除子系统
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Logging("删除子系统“{@p0}”")]
        public Result DeleteSubSystem(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "Id参数不可为空！";
                return res;
            }

            _subSystemRepository.Remove(ids);
            _subSystemRepository.Context.Commit();
            return res;
        }
        #endregion

        #region 获取一条主数据
        /// <summary>
        /// 获取一条主数据
        /// </summary>
        /// <returns></returns>
        public SubSystemDTO GetSubSystemInfo(Guid id)
        {
            return _subSystemRepository.Get(id).To<SubSystemDTO>();
        }
        #endregion

        #region 获取分页数据

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pf"></param>
        /// <returns></returns>
        public PagedResult<SubSystemDTO> GetSubSystemPaged(PagingFeature pf)
        {
            var paged = _subSystemRepository.Pagging(pf);

            var output = paged.To<SubSystem, SubSystemDTO>();


            return output;
        }
        #endregion
        #endregion
    }
}
